package com.yanqu.road.server.cmd.relatives;

import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.pb.relatives.RelativesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;


@Cmd(code = Protocol.S_RELATIVES_WRITE_LETTER, desc = "写信")
public class WriteLetterCmd extends AbstractCommand {

    private long relativesId;
    private String content;
    private int writeLetterNum;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RelativesProto.WriteLetterReqMsg reqMsg = RelativesProto.WriteLetterReqMsg.parseFrom(packet.getBytes());
        relativesId = reqMsg.getRelativesId();
        content = reqMsg.getContent();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //编辑限制
        GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_RELATIVES_WRITE_LETTER);
        if(null != gameEditLimit){
            long nowTime = System.currentTimeMillis();
            if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                return;
            }
        }
        RelativesInfo relativesInfo = player.getModule(RelativesModule.class).getRelativesInfo(relativesId);
        if (relativesInfo == null || relativesInfo.getStatus() == 0) {
            ret = GameErrorCode.E_RELATIVES_TIMES_NOT_ENOUGH;
            return;
        }
        if (StringUtils.isNullOrEmpty(content)) {
            ret = GameErrorCode.E_RELATIVES_EMPTY_LETTER;
            return;
        }
        if (ForbiddenWordMgr.isForbidden(content)){
            ret = GameErrorCode.E_FORBIDDEN_WORD;
            return;
        }
        ret = player.getModule(RelativesModule.class).writeLetter(relativesId, content);
        if (relativesInfo.getUserId1() == player.getUserId()) {
            writeLetterNum = relativesInfo.getLetterNum1();
        } else {
            writeLetterNum = relativesInfo.getLetterNum2();
        }
        player.getModule(AttributeModule.class).addAttribute(ePlayerAttrType.TotalSendLetterCount, 1);
    }

    @Override
    public void sendData(GamePlayer player) {
        RelativesProto.WriteLetterRespMsg.Builder respMsg = RelativesProto.WriteLetterRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (ret == 0) {
            respMsg.setWriteLetterNum(writeLetterNum);
        }
        player.sendPacket(Protocol.U_RELATIVES_WRITE_LETTER, respMsg);
    }
}
